﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Albon.Model;

namespace BLL
{
    public class CookieBLL
    {
        private readonly DAL.CookieDAL cookiedal = new DAL.CookieDAL();
        /// <summary>
        /// 添加cookie
        /// </summary>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public bool alterCookie(CookieInfo cookie)
        {
            return cookiedal.alterCookie(cookie) == 1;
        }
        /// <summary>
        /// 删除cookie   可以批量删除,只能删除未通过的cookie（先禁用，后删除）
        /// </summary>
        /// <param name="idlist"></param>
        /// <returns></returns>
        public int delCookie(string idlist)
        {
            return cookiedal.delCookie(idlist);
        }
        /// <summary>
        /// 修改cookie的状态    passed=1 通过 0 未通过
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        public void setState(int id,bool state)
        {
            cookiedal.setState(id,state);
        }
        /// <summary>
        /// 获取服务器上的cookies信息
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public DataTable getCookie(string username)
        {
            return cookiedal.getCookie(username).Tables[0];
        }





        /// <summary>
        /// 比较已有注册用户和允许注册的用户数，判断是否允许注册新用户,允许注册则返回空值 ，不允许则返回已注册的cookie
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public bool allowRegist(string username)
        {
            UserBLL userbll=new UserBLL();
            int maxUser=int.Parse(userbll.getUserByName(username).Rows[0]["maxUser"].ToString());
            if (cookiedal.getCookie(username).Tables[0].Rows.Count < maxUser)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 启用或者禁用cookie,传入cookieID和Passed的值
        /// </summary>
        /// <param name="cookie"></param>
        public void setState(CookieInfo cookie)
        {
            cookiedal.setState(cookie.ID, cookie.Passed);
        }
    }
}
